package com.wxxymaker.service.impl;

import com.wxxymaker.model.dao.MarkDao;
import com.wxxymaker.model.entity.Mark;
import com.wxxymaker.model.entity.Stage;
import com.wxxymaker.service.MarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Soft
 * @Date: 2018/5/6 1:28
 * @Desc: 打分控制业务层实现
 */
@Service
public class MarkServiceImpl implements MarkService {

    private final MarkDao dao;

    @Autowired
    public MarkServiceImpl(MarkDao dao) {
        this.dao = dao;
    }

    /**
     * 修改打分状态
     * stage 中的id为null时 即去除mark中的stage_id字段
     * @param can 打分状态
     * @param stage 打分阶段，带有id的stage
     */
    @Override
    @Transactional
    public boolean Switch(boolean can, Stage stage) {
        Stage s = stage;
        if (!can) {
            s = null;
        }
        return dao.setCanAndStage(can, s) > 0;
    }

    @Override
    @Transactional
    public boolean setCan(boolean can) {
        return dao.setCan(can) > 0;
    }

    @Override
    public boolean isCan() {
        Mark mark = this.getMark();
        if (null != mark) {
            return mark.getCan();
        }
        return false;
    }

    @Override
    public Stage getStage() {
        Mark mark = this.getMark();
        if (null != mark && null != mark.getStage()) {
            return mark.getStage();
        }
        return new Stage();
    }

    @Override
    public Mark getMark() {
        List<Mark> marks = dao.findAll();
        if (!marks.isEmpty()) {
            return marks.get(0);
        }
        return null;
    }
}
